package com.rongshu.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.rongshu.R;
import com.rongshu.view.home.entity.ShareInformation;
import com.rongshu.view.penpal.view.ShareDialogListener;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.utils.AppInstallUtils;
import com.widget.toast.ToastUtils;


/**
 * Created by zhengweis
 * 简单的将数据分享到第三方平台
 */

public class ShareBottomDialog extends Dialog {
    private LinearLayout shareWeChat, shareWeChatCircle, shareQQ, shareQZone, shareSina;
    //share type
    private static SHARE_MEDIA SHARE_TYPE;

    public ShareBottomDialog(Context context) {
        super(context, R.style.full_width_dialog_at_screen_bottom);
        setContentView(R.layout.fragment_invite_friend_dialog_view);
        initView();
    }

    private void initView() {
        shareWeChat = findViewById(R.id.share_wechat);
        shareWeChatCircle = findViewById(R.id.share_wechat_circle);
        shareQQ = findViewById(R.id.share_qq);
        shareQZone = findViewById(R.id.share_qzone);
        shareSina = findViewById(R.id.share_sina);
        findViewById(R.id.txt_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareBottomDialog.this.dismiss();
            }
        });
        WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        setCanceledOnTouchOutside(true);

    }

    public static class Builder implements View.OnClickListener {
        private Context mContext;
        //第三方Item点击监听
        private ShareDialogListener listener;
        //dialog消失监听
        private DialogInterface.OnDismissListener dismissListener;
        //待分享数据
        private ShareInformation.ResponseBean shareBean;
        private ShareBottomDialog dialog;

        public Builder(Context mContext) {
            this.mContext = mContext;
        }

        /**
         * 自定义 点击第三方分享监听
         */
        public Builder setSharePositionListener(ShareDialogListener listener) {
            this.listener = listener;
            return this;
        }

        /**
         * 设置分享数据,设置该选项不再需要设置 #setSharePositionListener(ShareDialogListener)
         */
        public Builder setShareData(ShareInformation.ResponseBean shareBean) {
            this.shareBean = shareBean;
            return this;
        }

        /**
         * dialog消失监听
         */
        public Builder setDialogDimissListener(DialogInterface.OnDismissListener dismissListener) {
            this.dismissListener = dismissListener;
            return this;
        }

        public ShareBottomDialog create() {
            if (dialog == null) {
                dialog = new ShareBottomDialog(mContext);
            }
            dialog.shareWeChat.setOnClickListener(this);
            dialog.shareWeChatCircle.setOnClickListener(this);
            dialog.shareQQ.setOnClickListener(this);
            dialog.shareQZone.setOnClickListener(this);
            dialog.shareSina.setOnClickListener(this);
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (dismissListener != null) {
                        dismissListener.onDismiss(dialog);
                    }
                }
            });
            return dialog;
        }

        private void shareThirdParty(SHARE_MEDIA type, ShareInformation.ResponseBean responseBean) {
            final UMWeb web = new UMWeb(responseBean.getShare_link());
            web.setTitle(responseBean.getTitle());//标题
            web.setThumb(new UMImage(mContext, responseBean.getImg()));  //缩略图
            web.setDescription(responseBean.getContent());//描述
            new ShareAction((Activity) mContext)
                    .setPlatform(type)//传入平台
                    .withMedia(web)
                    .setCallback(new UMShareListener() {
                        @Override
                        public void onStart(SHARE_MEDIA share_media) {

                        }

                        @Override
                        public void onResult(SHARE_MEDIA share_media) {
                        }

                        @Override
                        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                            ToastUtils.show(throwable.getMessage());
                        }

                        @Override
                        public void onCancel(SHARE_MEDIA share_media) {
                        }
                    }).share();
        }

        @Override
        public void onClick(View view) {
            if (dialog == null) {
                return;
            }
            switch (view.getId()) {
                case R.id.share_wechat:
                    if (AppInstallUtils.isWeixinAvilible(mContext)) {
                        SHARE_TYPE = SHARE_MEDIA.WEIXIN;
                    } else {
                        ToastUtils.show("\"微信\"不可用\n请检查手机是否安装\"微信\"");
                        return;
                    }
                    break;
                case R.id.share_wechat_circle:
                    if (AppInstallUtils.isWeixinAvilible(mContext)) {
                        SHARE_TYPE = SHARE_MEDIA.WEIXIN_CIRCLE;
                    } else {
                        ToastUtils.show("\"微信\"不可用\n请检查手机是否安装\"微信\"");
                        return;
                    }
                    break;
                case R.id.share_qq:
                    if (AppInstallUtils.isQQAvailable(mContext)) {
                        SHARE_TYPE = SHARE_MEDIA.QQ;
                    } else {
                        ToastUtils.show("\"QQ\"不可用\n请检查手机是否安装\"QQ\"");
                        return;
                    }
                    break;
                case R.id.share_qzone:
                    if (AppInstallUtils.isQQAvailable(mContext)) {
                        SHARE_TYPE = SHARE_MEDIA.QZONE;
                    } else {
                        ToastUtils.show("\"QQ\"不可用\n请检查手机是否安装\"QQ\"");
                        return;
                    }
                    break;
                case R.id.share_sina:
                    if (AppInstallUtils.isWBAvailable(mContext)) {
                        SHARE_TYPE = SHARE_MEDIA.SINA;
                    } else {
                        ToastUtils.show("\"新浪微博\"不可用\n请检查手机是否安装\"新浪微博\"");
                        return;
                    }
                    break;
            }
            if (listener != null) {
                listener.onShareClick(dialog, SHARE_TYPE);
            }
            if (shareBean != null) {
                dialog.dismiss();
                shareThirdParty(SHARE_TYPE, shareBean);
            }
        }
    }

}



